import requests
from pprint import pprint
import time
import xlwt
from pyquery import PyQuery as pq

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()

url = "https://www.jiucaigongshe.com/action/2022-04-06"
# res = requests.get(url=url)
# print(res.text)

# 关于样式
style_head = xlwt.XFStyle()
# 创建一个Excel
excel = xlwt.Workbook()
# 添加工作区
sheet = excel.add_sheet("数据")
# 标题信息
head = ["类别", "股票名称", "股票代码", "最新价", "涨跌幅", "解析"]
# head = ["股票名称", "股票代码", "最新价", "涨跌幅", "解析"]
for index, value in enumerate(head):
    sheet.write(0, index, value)
# 内容
content = []
ticai = []

driver.get(url=url)
# driver.find_element(By.XPATH, '//*[@id="__layout"]/div/div[2]/div/div/div/div[2]/div[1]').click()
time.sleep(2)
resp = driver.page_source
doc = pq(driver.page_source)

liss = doc('.module-box li').items()
# print(liss1)
for lii in liss:
    # print(lii)
    ticai.append((
        lii.find('.fs18-bold').text(),
        lii.find('.fs15-bold').text(),
        lii.find('.force-wrap').text(),
        lii.find('.number').text(),
        lii.find('.cred').text(),
        lii.find('.threeLine').text()
    ))

print(len(ticai))
print(ticai)

# lis = doc('.td-box li').items()
# print(lis)
for li in liss:
    # print(li.find('.fs18-bold').text(),
    #       li.find('.fs15-bold').text(),
    #       li.find('.force-wrap').text(),
    #       li.find('.number').text(),
    #       li.find('.cred').text(),
    #       li.find('.threeLine').text())
    content.append((
        li.find('.fs18-bold').text(),
        li.find('.fs15-bold').text(),
        li.find('.force-wrap').text(),
        li.find('.number').text(),
        li.find('.cred').text(),
        li.find('.threeLine').text()))
print(len(content))
print(content)
#
# for index, value in enumerate(ticai):
#     content[index] += tuple(value)

for index, value_list in enumerate(ticai, 1):
    for i, value in enumerate(value_list):
        sheet.write(index, i, value)

excel.save("./{}.xlsx".format(time.time()))
# print(li)
driver.close()
